/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sputty.classes;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import sputty.terminal.SwingTerminal;
import sputty.terminal.Term;

/**
 *
 * @author mauro
 */
public class RemoteConnection extends Thread {
    String hostname;
    String username;
    String password;
    Component parent;
    TerminalAreaExtended TerminalArea;
    SwingTerminal term;
    InputStream in;
    OutputStream out;
    String CurrentCommand = "";
    ConnectionSettings ConSet;
    JTabbedPane TabConnections;
    Connection conn;
    
    public RemoteConnection(Component parent, JTabbedPane TabConnections, ConnectionSettings ConSet){
        this.ConSet = ConSet;
        this.hostname = ConSet.getHost();
        this.username = ConSet.getUserName();
        this.password = ConSet.getPassword();
        this.parent = parent;
        this.TabConnections = TabConnections;
    }
    
    public void run(){
        conn = new Connection(hostname);
        boolean isAuthenticated = false;
        try{
            conn.connect();
            if(ConSet.getLoginMacro()){
                isAuthenticated = conn.authenticateWithPassword(username, password);
            }
            if(isAuthenticated){
                Session sess = conn.openSession();
                in = sess.getStdout();
                out = sess.getStdin();
                JPanel P = new JPanel(new java.awt.BorderLayout());
                /*TerminalArea = new MyTerminalArea();
                int x_width = TerminalArea.getWidth();
                int y_width = TerminalArea.getHeight();
                sess.requestPTY("vt100",x_width,y_width,0,0,null);
                sess.startShell();
                TerminalArea.setRows(x_width);
                TerminalArea.setColumns(y_width);
                TerminalArea.setBackground(Color.BLACK);
                TerminalArea.setForeground(Color.ORANGE);
                TerminalArea.setCaretColor(Color.ORANGE);
                TerminalArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 10));*/
                final OutputStream fout=out;
                final InputStream fin=in;
                final Session fsess=sess;
                sputty.terminal.Connection TermConn = new sputty.terminal.Connection() {

                    @Override
                    public InputStream getInputStream() {
                        return fin;
                    }

                    @Override
                    public OutputStream getOutputStream() {
                        return fout;
                    }
                    
                    @Override
                    public Session getCurrentSession(){
                        return fsess;
                    }

                    @Override
                    public void requestResize(Term term) {
                        if(fsess instanceof Session){
                            int c=term.getColumnCount();
                            int r=term.getRowCount();
                            /*try{
                                fsess.
                                fsess.requestPTY("vt100",c,r,c*term.getCharWidth(),r*term.getCharHeight(),null);
                            }catch(Exception e){
                                e.printStackTrace();
                            }*/
                        }
                    }

                    @Override
                    public void close() {
                        fsess.close();
                    }
                };
                term = new SwingTerminal();
                term.setConnection(TermConn);
                term.requestFocus();
                
                //TerminalArea = new TerminalAreaExtended();

                //JScrollPane SP = new JScrollPane(term);
                //SP.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                //SP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                
                P.add(term, java.awt.BorderLayout.CENTER);
                JScrollBar Bar = new JScrollBar(JScrollBar.VERTICAL);
                P.add(Bar, java.awt.BorderLayout.EAST);
                TabConnections.add(P);
                TabConnections.setTitleAt(TabConnections.getTabCount()-1, ConSet.getName());
                TabConnections.setSelectedIndex(TabConnections.getTabCount()-1);
                term.setParent(Bar);
                term.grabFocus();
                //TerminalArea.setInputStream(in);
                //TerminalArea.setOutputStream(out);
                //TerminalArea.setConnection(conn);
                /*
                KeyAdapter kl = new KeyAdapter(){
                    public void keyTyped(KeyEvent e){
                        int c = e.getKeyChar();
                        try{
                            out.write(c);
                            
                            if(c != '\n'){
                                CurrentCommand = CurrentCommand + (char)c;
                            }else{
                                //gestione comandi particolari tipo clear
                                CurrentCommand = "";
                            }
                        }catch (Exception e1){
                            e1.printStackTrace();
                        }
                        e.consume();
                    }
                };

                TerminalArea.addKeyListener(kl);
                TerminalArea.getCaret().setBlinkRate(500);
                TerminalArea.getCaret().setVisible(true);
                TerminalArea.setConnection(conn);*/
                //fsess.startShell();
                int x_width = term.getTermWidth();
                int y_width = term.getTermHeight();
                sess.requestPTY("vt100",term.getTermWidth(),term.getTermHeight(),0,0,null);
                sess.startShell();
                //term.setParent(SP);
                new RemoteConsumer(y_width, x_width, in, term).start();
            }else{
                JOptionPane.showMessageDialog(parent, "Connessione rifiutata, user o password errati", "Sputty", JOptionPane.ERROR_MESSAGE);
            }
        }catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(parent, e.getMessage(), "Sputty",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public InputStream getInputStream(){
        return this.in;
    }
    
    public OutputStream getOutputStream(){
        return this.out;
    }
    
    public Connection getConnection(){
        return this.conn;
    }
    
    
}
